package cn.xxm.strategy.pay.i18n.config;

import cn.xxm.strategy.pay.i18n.utils.HttpServletRequestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 实现本地化接口
 * @author XIEBO990
 */
public class FFLocaleResolver implements LocaleResolver {


    /**
     * 实现本地化核心方法，从请求参数中获取国际化标识
     * @param request
     * @return
     */
	@Override
	public Locale resolveLocale(HttpServletRequest request) {
        //String i18n= request.getParameter("internationnal");//app国际化标识，中文：zh_CN，英文：en_US等格式，前者代表语言，后者代表国家
		Locale locale =Locale.getDefault();//初始化是默认的
		try {
			Object obj = HttpServletRequestUtils.getAttribute(request, "international");
			if(obj==null) {
				return locale;
			}
			String i18n ="";
			if(obj instanceof String) {
				i18n =(String)obj;
			}else {
				return locale;
			}
			if(!StringUtils.isEmpty(i18n)){//判断是否为null
			    String[]spilt=i18n.split("_");
			    locale=new Locale(spilt[0],spilt[1]);//截取之后把新的属性封装给local
			}
		} catch (Exception e) {
		}
        return locale;
	}

	@Override
	public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
	}

}
